﻿$(document).ready(function () {

    var webServiceAddress = $('#MainContent_serverPathHiddenField').val() + "schedulerWebService.asmx/";
    var eventArray = new Array();
    var roomCode = $('#MainContent_roomDropDownList option:selected').val();

    $('#calendar').fullCalendar('removeEvents');

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        defaultView: 'month',
        editable: true,

        events: function (start, end, callback) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: webServiceAddress + "GetEventsForRoomInRange",
                data: "{ startDateRange: '" + NormalizeDateToString(start) + "', endDateRange: '" + NormalizeDateToString(end) + "', roomCode: '" + roomCode + "' }",
                dataType: "xml",
                async: false,
                success: function (data, textStatus, XMLHttpRequest) {
                    // Convert XML to JavaScript object
                    $(data).find("CalendarEvent").each(function () {
                        var event = new Object();

                        event.id = $(this).find("Id").text();
                        event.title = $(this).find("Title").text();
                        event.allDay = ($(this).find("AllDay").text() == "true") ? true : false;
                        event.start = $(this).find("Start").text();
                        event.end = $(this).find("End").text();
                        event.url = $(this).find("Url").text(); // Not needed.
                        event.className = $(this).find("ClassName").text();
                        event.editable = ($(this).find("Editable").text() == "true") ? true : false;
                        event.roomCode = $(this).find("RoomCode").text();
                        
                        eventArray.push(event);
                    });
                    callback(eventArray);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest + " Failure: " + textStatus + "->" + errorThrown);
                }
            });
        },

        eventDrop: function (event, delta) {
            alert(event.title + ' was moved ' + delta + ' days\n' + '(should probably update your database)');

            // adjust with delta then save
            SaveEventToDB(event);
        },

        loading: function (bool) {
            if (bool) $('#loading').show();
            else $('#loading').hide();
        },

        eventResize: function (event, dayDelta, minuteDelta, revertFunc) {

            alert(
                    "The end date of " + event.title + " has been moved " +
                    dayDelta + " days and " +
                    minuteDelta + " minutes."
                );

            if (!confirm("is this okay?")) {
                // Revert to old length or update database

                // adjust with deltas then save?
                SaveEventToDB(event);
            }

        }

    });

});